home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / RLBS8Y (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.6 KB  |  148 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.event.DocumentEvent;
  4. import java.awt.Component;
  5. import java.awt.Graphics;
  6. import java.awt.Rectangle;
  7. import java.awt.Shape;
  8.  
  9. class WrappedPlainView$WrappedLine extends View {
  10.    // $FF: synthetic field
  11.    WrappedPlainView this$0;
  12.    int nlines;
  13.  
  14.    // $FF: synthetic method
  15.    WrappedPlainView$WrappedLine(WrappedPlainView this$0, Element elem) {
  16.       super(elem);
  17.       this.this$0 = this$0;
  18.    }
  19.  
  20.    final int calculateLineCount() {
  21.       int nlines = 0;
  22.       int p1 = ((View)this).getEndOffset();
  23.  
  24.       int p;
  25.       for(int p0 = ((View)this).getStartOffset(); p0 < p1; p0 = p == p0 ? p1 : p) {
  26.          ++nlines;
  27.          p = this.this$0.calculateBreakPosition(p0, p1);
  28.       }
  29.  
  30.       return nlines;
  31.    }
  32.  
  33.    public float getPreferredSpan(int axis) {
  34.       switch (axis) {
  35.          case 0:
  36.             return (float)this.this$0.getWidth();
  37.          case 1:
  38.             if (this.nlines == 0 || this.this$0.widthChanging) {
  39.                this.nlines = this.calculateLineCount();
  40.             }
  41.  
  42.             int h = this.nlines * this.this$0.metrics.getHeight();
  43.             return (float)h;
  44.          default:
  45.             throw new IllegalArgumentException("Invalid axis: " + axis);
  46.       }
  47.    }
  48.  
  49.    public void paint(Graphics g, Shape a) {
  50.       Rectangle alloc = (Rectangle)a;
  51.       int y = alloc.y + this.this$0.metrics.getAscent();
  52.       int x = alloc.x;
  53.       int p1 = ((View)this).getEndOffset();
  54.  
  55.       for(int p0 = ((View)this).getStartOffset(); p0 < p1; y += this.this$0.metrics.getHeight()) {
  56.          int p = this.this$0.calculateBreakPosition(p0, p1);
  57.          this.this$0.drawLine(p0, p, g, x, y);
  58.          p0 = p == p0 ? p1 : p;
  59.       }
  60.  
  61.    }
  62.  
  63.    public Shape modelToView(int pos, Shape a) throws BadLocationException {
  64.       Rectangle alloc = (Rectangle)a;
  65.       alloc.height = this.this$0.metrics.getHeight();
  66.       alloc.width = 1;
  67.       int p1 = ((View)this).getEndOffset();
  68.  
  69.       for(int p0 = ((View)this).getStartOffset(); p0 < p1; alloc.y += alloc.height) {
  70.          int p = this.this$0.calculateBreakPosition(p0, p1);
  71.          if (pos >= p0 && pos < p) {
  72.             this.this$0.loadText(p0, pos);
  73.             alloc.x += Utilities.getTabbedTextWidth(this.this$0.lineBuffer, this.this$0.metrics, alloc.x, this.this$0, p0);
  74.             return alloc;
  75.          }
  76.  
  77.          p0 = p == p0 ? p1 : p;
  78.       }
  79.  
  80.       throw new BadLocationException((String)null, pos);
  81.    }
  82.  
  83.    public int viewToModel(float fx, float fy, Shape a) {
  84.       Rectangle alloc = (Rectangle)a;
  85.       ((View)this).getDocument();
  86.       int x = (int)fx;
  87.       int y = (int)fy;
  88.       if (y < alloc.y) {
  89.          return ((View)this).getStartOffset();
  90.       } else if (y > alloc.y + alloc.height) {
  91.          return ((View)this).getEndOffset() - 1;
  92.       } else {
  93.          alloc.height = this.this$0.metrics.getHeight();
  94.          int p1 = ((View)this).getEndOffset();
  95.  
  96.          for(int p0 = ((View)this).getStartOffset(); p0 < p1; alloc.y += alloc.height) {
  97.             int p = this.this$0.calculateBreakPosition(p0, p1);
  98.             if (y >= alloc.y && y < alloc.y + alloc.height) {
  99.                if (x < alloc.x) {
  100.                   return p0;
  101.                }
  102.  
  103.                if (x > alloc.x + alloc.width) {
  104.                   return p;
  105.                }
  106.  
  107.                int n = Utilities.getTabbedTextOffset(this.this$0.lineBuffer, this.this$0.metrics, alloc.x, x, this.this$0, p0);
  108.                return Math.min(p0 + n, p1 - 1);
  109.             }
  110.  
  111.             p0 = p == p0 ? p1 : p;
  112.          }
  113.  
  114.          return ((View)this).getEndOffset() - 1;
  115.       }
  116.    }
  117.  
  118.    public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  119.       int n = this.calculateLineCount();
  120.       if (this.nlines != n) {
  121.          this.nlines = n;
  122.          this.this$0.preferenceChanged(this, false, true);
  123.       }
  124.  
  125.       if (a != null) {
  126.          Component c = ((View)this).getContainer();
  127.          Rectangle alloc = (Rectangle)a;
  128.          c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
  129.       }
  130.  
  131.    }
  132.  
  133.    public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  134.       int n = this.calculateLineCount();
  135.       if (this.nlines != n) {
  136.          this.nlines = n;
  137.          this.this$0.preferenceChanged(this, false, true);
  138.       }
  139.  
  140.       if (a != null) {
  141.          Component c = ((View)this).getContainer();
  142.          Rectangle alloc = (Rectangle)a;
  143.          c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
  144.       }
  145.  
  146.    }
  147. }
  148.